package priv.lirenhe.javasuper.thread.ticket;

public class TicketRunWindow implements Runnable{

	public TicketRunWindow() {
		
	}
	
	private int tickets = 100;
	Object lock = new Object();
	
	@Override
	public void run(){
		/*
		while(true){
			if(tickets > 0){
				Thread th = Thread.currentThread();
				String th_name = th.getName();
				if(th_name.equals("window01")){
					System.out.println(tickets);
					try {
						Thread.sleep(1);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
				System.out.println(th_name + "正在发售： " + tickets-- + "--张票");
			}
		}
		*/
		
		while(true){
			/*synchronized (lock) {// 监听模式
				if(tickets > 0){
				Thread th = Thread.currentThread();
				String th_name = th.getName();
				System.out.println(th_name + "正在发售： " + tickets-- + "--张票");
			}*/
			soldTicket();
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	synchronized protected void soldTicket(){
		if(tickets > 0){
			Thread th = Thread.currentThread();
			String th_name = th.getName();
			System.out.println(th_name + "正在发售： " + tickets-- + "--张票");
		}
	}
	
	

}
